package com.misc.tunewsreader;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

public class Preferences extends PreferenceActivity {
	private Context context;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;

		addPreferencesFromResource(R.xml.preferences);
		setContentView(R.layout.settings);

		ActionBar bar = (ActionBar) findViewById(R.id.actionbar);
		bar.setTitle(this.getString(R.string.settings));
		bar.addAction(new DoneAction(this, R.drawable.bar_accept));

		String app_version = "1.0";
		try {
			app_version = this.getPackageManager().getPackageInfo(
					this.getPackageName(), 0).versionName;
		} catch (NameNotFoundException exception) {
			exception.printStackTrace();
		}
		final String about_title = context.getString(R.string.about_title)
				.replace("[version]", app_version);

		findPreference("pref_about").setOnPreferenceClickListener(
				new OnPreferenceClickListener() {
					@Override
					public boolean onPreferenceClick(Preference preference) {
						Builder about_dlg = new AlertDialog.Builder(context)
								.setTitle(about_title)
								.setMessage(
										context.getString(R.string.about_text))
								.setNeutralButton(
										context.getString(android.R.string.ok),
										null);
						about_dlg.show();
						return true;
					}
				});

		findPreference("pref_changelog").setOnPreferenceClickListener(
				new OnPreferenceClickListener() {
					@Override
					public boolean onPreferenceClick(Preference preference) {
						Builder changelog_dlg = new AlertDialog.Builder(context)
								.setTitle(
										context.getString(R.string.changelog_title))
								.setMessage(
										context.getString(R.string.changelog_text))
								.setNeutralButton(
										context.getString(android.R.string.ok),
										null);
						changelog_dlg.show();
						return true;
					}
				});

		// show help screen
		findPreference("pref_help").setOnPreferenceClickListener(
				new OnPreferenceClickListener() {
					@Override
					public boolean onPreferenceClick(Preference preference) {
						startActivityForResult(new Intent(
								getApplicationContext(), Help.class), 0);
						return true;
					}
				});

		// register for preference change
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
			@Override
			public void onSharedPreferenceChanged(
					SharedPreferences sharedPreferences, String key) {
				updateInfo();
			}
		});

		updateInfo();
	}

	public void updateInfo() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		findPreference("pref_username").setSummary(
				prefs.getString("pref_username", ""));
		findPreference("pref_mail")
				.setSummary(prefs.getString("pref_mail", ""));
		findPreference("pref_signature").setSummary(
				prefs.getString("pref_signature", ""));
		findPreference("pref_sync_key").setSummary(
				prefs.getString("pref_sync_key", ""));

		// display selected font size
		String[] fontsize = getApplicationContext().getResources()
				.getStringArray(R.array.fontsize);
		String[] fontsize_value = getApplicationContext().getResources()
				.getStringArray(R.array.fontsize_values);
		String pref_fontsize = prefs.getString("pref_fontsize", "100%");
		String human_fontsize = "";
		for (int i = 0; i < fontsize_value.length; i++) {
			if (fontsize_value[i].equals(pref_fontsize)) {
				human_fontsize = fontsize[i];
				break;
			}
		}
		findPreference("pref_fontsize").setSummary(human_fontsize);

	}

	public static class DoneAction extends AbstractAction {
		private Context mContext;

		public DoneAction(Context context, int drawable) {
			super(drawable);
			mContext = context;
		}

		@Override
		public void performAction(View view) {
			((Preferences) mContext).setResult(0);
			((Preferences) mContext).finish();
		}
	}

}
